﻿using Hospital.IService;
using Hospital.Model.type;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class DepartmentController : ControllerBase
    {
        private IDepartmentService _iDepartmentService;

        public DepartmentController(IDepartmentService iDepartmentService)
        {
            this._iDepartmentService = iDepartmentService;
        }

        // 获取科室
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResult>> GetDepartment(int id)
        {
            try
            {
                var department = await _iDepartmentService.FindAsync(id);
                if (department != null) return ApiResultHelper.Success(department);
                return ApiResultHelper.Error("未找到该科室");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("获取科室出现错误");
            }
        }

        // 获取所有科室
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetDepartments()
        {
            try
            {
                var departments = await _iDepartmentService.QueryAsync();
                if (departments != null) return ApiResultHelper.Success(departments);
                return ApiResultHelper.Error("未找到数据");
            }
            catch
            {
                return ApiResultHelper.Error("查询时出现错误");
            }
        }

        // 添加科室
        [HttpPost]
        public async Task<ActionResult<ApiResult>> CreateDepartment([FromBody] Department department)
        {
            try
            {
                bool b = await _iDepartmentService.CreateAsync(department);
                if (b) return ApiResultHelper.Success("服务器添加科室成功", department);
                return ApiResultHelper.Error("服务器添加科室失败");
            }
            catch
            {
                return ApiResultHelper.Error("服务器添加科室时出现错误");
            }
        }

        // 更新科室
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> UpdateDepartment(int id, [FromBody] Department department)
        {
            try
            {
                if (id != department.Id)
                {
                    return BadRequest();
                }
                else
                {
                    bool b = await _iDepartmentService.EditAsync(department);
                    if (b) return ApiResultHelper.Success("服务器修改科室成功", department);
                    return ApiResultHelper.Error("服务器修改科室失败");
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("服务器修改科室出现错误");
                throw;
            }
        }

        // 删除科室
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteDepartment(int id)
        {
            try
            {
                bool b = await _iDepartmentService.DeleteAsync(id);
                if (b) return ApiResultHelper.Success("删除科室成功");
                return ApiResultHelper.Error("删除科室失败");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("删除科室错误");
                throw;
            }
        }
    }

}
